<?php
/**
 * Created by Adon.
 * User: Adon
 * Date: 2017/8/18
 * Time: 14:17
 */

namespace app\modules\mch\controllers;

use app\models\Publish;
use app\modules\mch\models\Model;
use yii\data\Pagination;
class PublishController extends Controller
{
    public function actionIndex($cat_id = 1,$page=1)
    {


        $quest = Publish::find()->where([
            'type' => $cat_id,
        ]);
        $list=$quest->orderBy('create_time DESC')->all();
        $count=$quest->count();
        $pagination = new Pagination(['totalCount' => $count, 'page' =>$page-1]);

        foreach ($list as $k =>$v){
            if($v['type']==1)$type="我要买";
            if($v['type']==2)$type="我要卖";
            if($v['type']==3)$type="我要服务";

            if($v['state']==1)$state="已接受";
            if($v['state']==2)$state="未接受";
            if($v['state']==3)$state="处理中";
            if($v['state']==4)$state="已完成";
            $v['type']=$type;
            $v['state']=$state;
        }
        /*if (empty($list) && $cat_id == 1) {
            $item = new Publish();
            $item->type = 1;
            $item->title = '我要买';
            $list[] = $item;
        }*/
        return $this->render('index', [
            'list' => $list,
            'cat_id' => $cat_id,
            'pagination'=>$pagination,
            'row_count'=>$count
        ]);
    }
//保存
    public function actionEdit($id)
    {

        $model = Publish::findOne([
            'id' => $id,
        ]);
        if (\Yii::$app->request->isPost) {
            $state = \Yii::$app->request->post('state');
            $update=\Yii::$app->db->createCommand()->update('cshopmall_publish', ['state' => $state], "id = {$id}")->execute();
            if($update>0){
                return [
                    'code' => 0,
                    'msg' => '保存成功',
                ];
            }else{
                return [
                    'code' => 1,
                    'msg' => '保存失败',
                ];
            }
        }


        return $this->render('edit', [
            'model' => $model,
        ]);


    }
    //审核
    public function actionAudit($id){
        $model = Publish::findOne([
            'id' => $id,
        ]);
        if ($model) {
            if($model['audit'] = 2){
                $update=\Yii::$app->db->createCommand()->update('cshopmall_publish', ['audit' => 1], "id = {$id}")->execute();
              if($update>0){
                  return [
                      'code' => 0,
                      'msg' => '审核通过',
                  ];
              }
            }

        }

    }

}
